package com.jic.finance.controller;

import com.alibaba.fastjson.JSON;
import com.jic.finance.base.BaseResult;
import com.jic.finance.enums.FinanceEmun;
import com.jic.finance.exception.FinanceException;

public class BaseController {

    protected String execute(Service service){
        BaseResult result = new BaseResult<>();
        try {
            //服务调用
            Object obj = service.service();
            result.setCode(FinanceEmun.SUCCESS.getCode());
            result.setMsg(FinanceEmun.SUCCESS.getMessage());
            result.setData(obj);
        }catch (FinanceException fe){
            fe.printStackTrace();
            result.setCode(fe.getFinanceEmun().getCode());
            result.setMsg(fe.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(FinanceEmun.UNKOWN_EXECEPTION.getCode());
            result.setMsg(FinanceEmun.UNKOWN_EXECEPTION.getMessage());
        }
        return JSON.toJSONString(result);
    }

    protected interface Service{
        Object service();
    }

}
